function graphsVec=plotStates(statesMat,sampleStru,inStru,nameStru)
% =========================================================================
% plotStates(statesMAT,sampleStru,inStru,nameStru);
% Can be used to plot booth Smooth States and Innovations 
%
%% I. Inputs 
%
%% I.1 *statesMat* [Nobs NS] matrix of *states*.
% 
% If desire to plot only some columns, can use extract below 
% 
%% I.2 *sampleStru*
%
%       *.sampleVec.*
% 
%% I.3 *inStru*  
%
% Structure with inputs for graphing as well as additional inputs 
% 
%% I.3.a inStru Required
%
%   *.suptitle*   String to add to the top of each graph 
%
%% I.3.b inStru Optional 
%
% *.extract* Names of these columns to be plotted only and extracted
%            from the total columns (else all columns plotted)

%    *.subtitle* String to add to the bottom of each graph (default is
%    none)
%    
%    *.subtitleFontSize*, optional FontSize of subtitle (defaults is 10) 
%
%
%    *.NXtickValues* Number of ticks for x axis (default is 9) 
%
%    *.NRow*       Number of rows per graph, (default here is 4)  
%
%    *.NCols*      Number of columns per graph (default here is 1) 
%    
%% I.4 *nameStru* 
% 
% Structure with Names of columns and rows 
%
% *nameStru.colNames*  Names of ALL columns (shocks), if extract is on,
% will be trimmed inside 
%
% Alejandro Justiniano January 21st 2010
% =========================================================================
%% 1. Dimensions
dim=size(statesMat);
%% 2. Check sizes
if length(sampleStru.sampleVec)~=dim(1);
    error('sampleVec does not match row dimension');
end
if length(nameStru.colNames)~=dim(2);
    error('colNames does not match column dimension');
end

%% 3. Assign Defaults to inStru
if isempty(inStru)==true || isfield(inStru,'suptitle')==false;
    error('Need define inStru.suptitle, suptitle for all Pages');
end 
% Number of Years to Plot 
if isfield(inStru,'NXtickValues')==false || isempty(inStru.NXtickValues)
    if unique(fix(sampleStru.sampleVec)) <= 8
        inStru.NXtickValues=length(unique(fix(sampleStru.sampleVec)));
    else
        inStru.NXtickValues=8;
    end
end
if isfield(inStru,'xtickValues')==false;
    junk=assignXAxis(sampleStru.sampleVec,inStru.NXtickValues);
    inStru.xtickValues=junk.xtickValues;
    inStru.xtickLabels=junk.xtickLabels;
end
inStru=plotAssignDefaults(inStru);

%% 4. Extract Columns
if isfield(inStru,'extract');
    extractPos=cellposition(inStru.extract,nameStru.colNames);
    nameStru.colNames=inStru.extract;
    statesMat=statesMat(:,extractPos);
    dim=size(statesMat);
end

%% 5. Assign Fields to inStru
if ~isempty(inStru.axesFontSize);
    set(0,'DefaultAxesFontSize',inStru.axesFontSize);
end
inStru.flagNoLegend=1;
inStru.legendFontSize=12;
inStru.xOffset=0.01;
inStru.yOffset=0.01;
inStru.titleFontSize=14; 
inStru.suptitleFontSize=16; 
inStru.flagBoxOff=1;
inStru.lineWidths=1.5;
inStru.colors=[0 0 1];
%% 6. Set Row and Column Dimensions
if isfield(inStru,'NRows')==false || isfield(inStru,'NCols')==false
    if dim(2) <= 4
        inStru.NRows=dim(2);
    else
        inStru.NRows=4;
    end
    inStru.NCols=1;
end
%% 8. Begin Loop
NGraphs=ceil(dim(2)/(inStru.NRows*inStru.NCols));
graphsVec=zeros(NGraphs,1); 
NSubplots=dim(2);
ISubplots=0;
IGraphs  =0;
ICounter =0;
for ii=1:NSubplots
    if ISubplots==0
        IGraphs=IGraphs+1;
        graphsVec(IGraphs)=figure;
    end
    ISubplots=ISubplots+1;
    ICounter =ICounter+1;
    subplot(inStru.NRows,inStru.NCols,ISubplots);
    plot3sampSub(sampleStru.sampleVec,statesMat(:,ii),nameStru.colNames{ii},1,inStru);
    %% If End SubPlots, put suptitle and subtitle, if defined
    if ISubplots==inStru.NRows*inStru.NCols || ii==NSubplots
        ISubplots=0;
        if NGraphs==1
            supHand=suptitle(inStru.suptitle);
        else
            supHand=suptitle([char(inStru.suptitle),'   (',num2str(IGraphs),' of ',...
                num2str(NGraphs),')']);
        end
        set(supHand,'FontSize',inStru.suptitleFontSize,'FontName',inStru.FontName,...
            'FontWeight',inStru.suptitleFontWeight);
        set(graphsVec(IGraphs),'PaperPosition',inStru.paperPosition); 
        if isfield(inStru,'subtitle')==true; 
            [~,inStru]=ch_field(inStru,'subtitleFontSize',10);
            subHand=subtitulo(inStru.subtitle); 
            set(subHand,'FontSize',inStru.subtitleFontSize,'FontName',inStru.FontName); 
        end 
    end
end
if ~isempty(inStru.axesFontSize);
    set(0,'DefaultAxesFontSize','remove');
end
